﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using tiDE.Settings;
using System.Diagnostics;

namespace tiDE.KeyboardManager
{
    public partial class KeyboardShortcutsDialog : Form
    {
        public KeyboardShortcutsDialog()
        {
            InitializeComponent();
            StartEnumerateNodes(tiDEWindow.ThisInstance.menuStrip1);
        }

        void StartEnumerateNodes(MenuStrip MenuStrip)
        {
            foreach (ToolStripMenuItem item in MenuStrip.Items)
            {
                treeView1.Nodes.Add(EnumerateNodes(new TreeNode(item.Text), item));
            }
        }

        TreeNode EnumerateNodes(TreeNode startNode, ToolStripMenuItem ToolStrip)
        {
            TreeNode returnNode = new TreeNode(ToolStrip.Text);
            for (int i = 0; i < ToolStrip.DropDownItems.Count; i++)
            {
                ToolStripDropDownItem item = ToolStrip.DropDownItems[i] as ToolStripDropDownItem;
                if (item == null)
                    continue;
                returnNode.Nodes.Add(EnumerateNodes(returnNode, item));
            }
            return returnNode;
        }

        TreeNode EnumerateNodes(TreeNode startNode, ToolStripDropDownItem ToolStrip)
        {
            TreeNode returnNode = new TreeNode(ToolStrip.Text);
            for (int i = 0; i < ToolStrip.DropDownItems.Count; i++)
            {
                ToolStripDropDownItem item = ToolStrip.DropDownItems[i] as ToolStripDropDownItem;
                if (item == null)
                    continue;
                returnNode.Nodes.Add(EnumerateNodes(returnNode, item));
            }
            return returnNode;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count != 0)
            {
                groupBox1.Enabled = false;
                return;
            }
            List<string> path = new List<string>();
            path.Add(e.Node.Text);
            TreeNode parent = e.Node.Parent;
            while (parent != null)
            {
                path.Add(parent.Text);
                parent = parent.Parent;
            }
            path.Reverse();
            shortcut = tiDEWindow.GlobalSettings.ShortcutManager.GetByPath(path.ToArray());
            if (shortcut == null)
            {
                shortcut = new KeyboardShortcut();
                shortcut.MenuPath = path.ToArray();
            }
            textBox1.Text = MakeShortcutString(shortcut);
            labelMenuOption.Text = e.Node.Text;
            groupBox1.Enabled = true;
        }

        KeyboardShortcut shortcut;

        string MakeShortcutString(KeyboardShortcut shortcut)
        {
            if (shortcut.Key == Keys.None)
                return "";
            string final = "";
            if (shortcut.IsControl)
            {
                final = "Ctrl+";
            }
            if (shortcut.IsShift)
            {
                final += "Shift+";
            }
            if (shortcut.IsAlt)
            {
                final += "Alt+";
            }
            if (shortcut.Key.ToString() != "ControlKey" &&
                shortcut.Key.ToString() != "ShiftKey" &&
                shortcut.Key.ToString() != "Menu")
                final += shortcut.Key.ToString();
            return final;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            shortcut.Key = e.KeyCode;
            if ((shortcut.Key & Keys.ControlKey) == Keys.ControlKey)
                shortcut.Key = shortcut.Key | Keys.ControlKey;
            if ((shortcut.Key & Keys.Menu) == Keys.Menu)
                shortcut.Key = shortcut.Key | Keys.Menu;
            if ((shortcut.Key & Keys.ShiftKey) == Keys.ShiftKey)
                shortcut.Key = shortcut.Key | Keys.ShiftKey;
            e.SuppressKeyPress = true;
            shortcut.IsShift = e.Shift;
            shortcut.IsControl = e.Control;
            shortcut.IsAlt = e.Alt;
            textBox1.Text = MakeShortcutString(shortcut);
            labelOverwrite.Visible = tiDEWindow.GlobalSettings.ShortcutManager.GetByShortcut(shortcut) != null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tiDEWindow.GlobalSettings.ShortcutManager.UpdateShortcut(shortcut);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            shortcut.Key = Keys.None;
            shortcut.IsAlt = false;
            shortcut.IsShift = false;
            shortcut.IsControl = false;
            textBox1.Text = MakeShortcutString(shortcut);
        }
    }
}
